<%@ page pageEncoding="UTF-8"%>
<%@ include file="/common/token.jsp"%>
<%@ include file="/common/operatehistory.jsp"%>
<%!
	// 查询热门科室,按照关注数量,已关注的不显示
	String hot(Integer uid, Input input) {
		Integer hospital = input.getInt("hospital", -1); // 医院id
		Integer page = input.getInt("page", 1); // 下拉刷新
		Integer itemCount = input.getInt("item_count", 6); // 上拉加载
		if (page <= 0 || itemCount <= 0) {
			return new Message(30041, "参数不合法，请输入正确的页码或限值").toJson();
		}
		org.hibernate.StatelessSession ss = DBTools.db("basedb").getSf().openStatelessSession();
		org.hibernate.SQLQuery sqlQuery = ss.createSQLQuery(String.format("select * from (SELECT h.id AS id,h. NAME AS dept_name,h.descr AS hospital_name,h.hospital AS dept_id,count(o.oid) AS attention FROM hospital AS h,operate_history AS o WHERE h.id = o.oid AND o. STATUS = 1 AND h. STATUS = 1 AND o.category = 7 GROUP BY o.oid ) as t where t.id not in(select o.oid from operate_history as o where o.status=1 and o.uid=%s and o.category=7) order by t.attention desc", new Object[]{uid}));
		List<Map> depts = sqlQuery.setFirstResult((page - 1) * itemCount).setMaxResults(itemCount).setResultTransformer(org.hibernate.transform.Transformers.ALIAS_TO_ENTITY_MAP).list();
		List<Map> rankCount = ss.createSQLQuery(String.format("select count(*) as size from (SELECT h.id AS id,h. NAME AS dept_name,h.descr AS hospital_name,h.hospital AS dept_id,count(o.oid) AS attention FROM hospital AS h,operate_history AS o WHERE h.id = o.oid AND o. STATUS = 1 AND h. STATUS = 1 AND o.category = 7 GROUP BY o.oid ) as t where t.id not in(select o.oid from operate_history as o where o.status=1 and o.uid=%s and o.category=7) order by t.attention desc", new Object[]{uid})).setResultTransformer(org.hibernate.transform.Transformers.ALIAS_TO_ENTITY_MAP).list();
		Integer size =  Tools.getInt(isEmpty(rankCount)?"0":(rankCount.get(0).get("size") + ""));
		if (ss != null) 
			ss.close();
		Message msg = new Message(0);
		Map resultMap = new HashMap();
		resultMap.put("rank", depts);
		resultMap.put("current_page", page);
		resultMap.put("page_count",  size%itemCount == 0?(size/itemCount):(size/itemCount + 1));
		resultMap.put("item_count",  itemCount);
		if (isEmpty(depts))
			msg.setErrcodeAndErrmsg(30041, "没有更多了");
		msg.put("data", resultMap);
		return msg.toJson();
	}
%>
<%
	Tools.encryptPrint(out, hot(token.getUid(), input));
%>
